% =========================================================================
% Schniepp Lab, 2018-2021
% Find the optimized coefficient for Raman spectra fitting
% parGuess contains the coeffients for the model polypeptide data
% =========================================================================

function [coeffcients, resnorm, resid, J] = optimizeCoeffwithMore(coeff_guess, pesudo_x, expData, lb, ub, mode, eTimes)
    if strcmp(mode, 'Cal')
        % set up fitting options
        options = optimoptions(@lsqcurvefit,'MaxFunEvals',eTimes,'MaxIter',25000,'Display','off');
        options.TolFun = 1.0e-10;
        
        % call lsqcurvefit to perform the fitting
        [coeffcients, resnorm, resid, ~, ~, ~, J] = lsqcurvefit(@get_pep_sum, coeff_guess, pesudo_x, expData, lb, ub, options);
    else
        % This branch is only used for quick evaluation, without the actual
        % fitting
        coeffcients = coeff_guess;
        resnorm = 0;
        resid = 0;
        J = 0;
    end
end